﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using MySql.Data.MySqlClient;

public class DAL_tbl_qcsicomill_particlesize
{
    public DAL_tbl_qcsicomill_particlesize()
    {

    }



    public List<tbl_qcsicomill_particlesize> select(String Particle_Size_ID)
    {
        List<tbl_qcsicomill_particlesize> list = new List<tbl_qcsicomill_particlesize>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select Particle_Size_ID,Work_Order_Number,DRUM_NUMBER,D10,D50,D90,D99,Remark,IS_ACTIVE,CREATED_BY,CREATED_DATE,UPDATED_BY,UPDATED_DATE from tbl_qcsicomill_particlesize where Particle_Size_ID=@Particle_Size_ID";
        cmd.Parameters.AddWithValue("@Particle_Size_ID", Particle_Size_ID);
        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_qcsicomill_particlesize()
            {
                Particle_Size_ID = dr["Particle_Size_ID"].ToString(),
                Work_Order_Number = dr["Work_Order_Number"].ToString(),
                DRUM_NUMBER = dr["DRUM_NUMBER"].ToString(),
                D10 = dr["D10"].ToString(),
                D50 = dr["D50"].ToString(),
                D90 = dr["D90"].ToString(),
                D99 = dr["D99"].ToString(),                
                Remark = dr["Remark"].ToString(),
                IS_ACTIVE = Convert.ToBoolean(dr["IS_ACTIVE"]),
                CREATED_BY = dr["CREATED_BY"].ToString(),
                CREATED_DATE = dr["CREATED_DATE"].toDateTimeWithoutError(),
                UPDATED_BY = dr["UPDATED_BY"].ToString(),
                UPDATED_DATE = dr["UPDATED_DATE"].toDateTimeWithoutError()
            });
        }

        return list;
    }



    public List<tbl_qcsicomill_particlesize> selectAll()
    {
        List<tbl_qcsicomill_particlesize> list = new List<tbl_qcsicomill_particlesize>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select Particle_Size_ID,Work_Order_Number,DRUM_NUMBER,D10,D50,D90,D99,Remark,IS_ACTIVE,CREATED_BY,CREATED_DATE,UPDATED_BY,UPDATED_DATE from tbl_qcsicomill_particlesize Order By Particle_Size_ID";

        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_qcsicomill_particlesize()
            {
                Particle_Size_ID = dr["Particle_Size_ID"].ToString(),
                Work_Order_Number = dr["Work_Order_Number"].ToString(),
                DRUM_NUMBER = dr["DRUM_NUMBER"].ToString(),                
                D10 = dr["D10"].ToString(),
                D50 = dr["D50"].ToString(),
                D90 = dr["D90"].ToString(),
                D99 = dr["D99"].ToString(),             
                Remark = dr["Remark"].ToString(),
                IS_ACTIVE = Convert.ToBoolean(dr["IS_ACTIVE"]),
                CREATED_BY = dr["CREATED_BY"].ToString(),
                CREATED_DATE = dr["CREATED_DATE"].toDateTimeWithoutError(),
                UPDATED_BY = dr["UPDATED_BY"].ToString(),
                UPDATED_DATE = dr["UPDATED_DATE"].toDateTimeWithoutError()
            });
        }

        return list;
    }

    public DataTable selectAllForGrid()
    {
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = @"Select Particle_Size_ID,Work_Order_Number,DRUM_NUMBER,D10,D50,D90,D99,Remark,CASE WHEN IS_ACTIVE =1 THEN 'True' ELSE 'False' END as 'IS_ACTIVE',CREATED_BY,CREATED_DATE as CREATED_DATE,UPDATED_BY,UPDATED_DATE from tbl_qcsicomill_particlesize Order By Particle_Size_ID desc";

        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        return dt;
    }
    public bool insert(List<tbl_qcsicomill_particlesize> list)
    {
        bool ret = false;
        List<MySqlCommand> cmds = new List<MySqlCommand>();
        string sql = @"Insert into tbl_qcsicomill_particlesize(Work_Order_Number, DRUM_NUMBER, D10, D50, D90, D99, Remark, IS_ACTIVE, CREATED_BY, CREATED_DATE)
                        values(@Work_Order_Number, @DRUM_NUMBER, @D10, @D50, @D90, @D99, @Remark, @IS_ACTIVE, @CREATED_BY, @CREATED_DATE)";

        foreach (tbl_qcsicomill_particlesize row in list)
        {
            MySqlCommand cmd = new MySqlCommand(sql);

            cmd.Parameters.AddWithValue("@Work_Order_Number", row.Work_Order_Number);
            cmd.Parameters.AddWithValue("@DRUM_NUMBER", row.DRUM_NUMBER);            
            cmd.Parameters.AddWithValue("@D10", row.D10);
            cmd.Parameters.AddWithValue("@D50", row.D50);
            cmd.Parameters.AddWithValue("@D90", row.D90);
            cmd.Parameters.AddWithValue("@D99", row.D99);            
            cmd.Parameters.AddWithValue("@Remark", row.Remark);
            cmd.Parameters.AddWithValue("@IS_ACTIVE", true);
            cmd.Parameters.AddWithValue("@CREATED_BY", HttpContext.Current.User.Identity.Name);
            cmd.Parameters.AddWithValue("@CREATED_DATE", DateTime.Now);
            cmds.Add(cmd);
        }
        ret = DBUtil.ExecuteNonQuery(cmds);
        return ret;
    }

    public int insert(String Work_Order_Number, String DRUM_NUMBER, String D10, String D50, String D90, String D99, String Remark, bool IS_ACTIVE, String CREATED_BY, DateTime CREATED_DATE)
    {
        int i = 0;
        string sql = @"Insert into tbl_qcsicomill_particlesize(Work_Order_Number, DRUM_NUMBER, D10, D50, D90, D99, Remark, IS_ACTIVE, CREATED_BY, CREATED_DATE)
                        values(@Work_Order_Number, @DRUM_NUMBER, @D10, @D50, @D90, @D99, @Remark, @IS_ACTIVE, @CREATED_BY, @CREATED_DATE)";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@Work_Order_Number", Work_Order_Number);
        cmd.Parameters.AddWithValue("@DRUM_NUMBER", DRUM_NUMBER);        
        cmd.Parameters.AddWithValue("@D10", D10);
        cmd.Parameters.AddWithValue("@D50", D50);
        cmd.Parameters.AddWithValue("@D90", D90);
        cmd.Parameters.AddWithValue("@D99", D99);        
        cmd.Parameters.AddWithValue("@Remark", Remark);
        cmd.Parameters.AddWithValue("@IS_ACTIVE", true);
        cmd.Parameters.AddWithValue("@CREATED_BY", HttpContext.Current.User.Identity.Name);
        cmd.Parameters.AddWithValue("@CREATED_DATE", DateTime.Now);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }


    public bool update(List<tbl_qcsicomill_particlesize> list)
    {
        bool ret = false;
        List<MySqlCommand> cmds = new List<MySqlCommand>();
        string sql = @"Update tbl_qcsicomill_particlesize SET Work_Order_Number=@Work_Order_Number, DRUM_NUMBER=@DRUM_NUMBER, D10=@D10, D50=@D50, D90=@D90, D99=@D99, Remark=@Remark, IS_ACTIVE=@IS_ACTIVE, UPDATED_BY=@UPDATED_BY, UPDATED_DATE=@UPDATED_DATE WHERE Particle_Size_ID=@Particle_Size_ID";

        foreach (tbl_qcsicomill_particlesize row in list)
        {
            MySqlCommand cmd = new MySqlCommand(sql);

            cmd.Parameters.AddWithValue("@Particle_Size_ID", row.Particle_Size_ID);
            cmd.Parameters.AddWithValue("@Work_Order_Number", row.Work_Order_Number);
            cmd.Parameters.AddWithValue("@DRUM_NUMBER", row.DRUM_NUMBER);
            cmd.Parameters.AddWithValue("@D10", row.D10);
            cmd.Parameters.AddWithValue("@D50", row.D50);
            cmd.Parameters.AddWithValue("@D90", row.D90);
            cmd.Parameters.AddWithValue("@D99", row.D99);
            cmd.Parameters.AddWithValue("@Remark", row.Remark);
            cmd.Parameters.AddWithValue("@IS_ACTIVE", true);
            cmd.Parameters.AddWithValue("@UPDATED_BY", HttpContext.Current.User.Identity.Name);
            cmd.Parameters.AddWithValue("@UPDATED_DATE", DateTime.Now);

            cmds.Add(cmd);
        }
        ret = DBUtil.ExecuteNonQuery(cmds);
        return ret;
    }


    public int update(String Particle_Size_ID, String Work_Order_Number, String DRUM_NUMBER, String D10, String D50, String D90, String D99, String Remark, bool IS_ACTIVE, String UPDATED_BY, DateTime UPDATED_DATE)
    {
        int i = 0;
        string sql = @"Update tbl_qcsicomill_particlesize SET Work_Order_Number=@Work_Order_Number, DRUM_NUMBER=@DRUM_NUMBER, D10=@D10, D50=@D50, D90=@D90, D99=@D99, Remark=@Remark, IS_ACTIVE=@IS_ACTIVE, UPDATED_BY=@UPDATED_BY, UPDATED_DATE=@UPDATED_DATE WHERE Particle_Size_ID=@Particle_Size_ID";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@Particle_Size_ID", Particle_Size_ID);
        cmd.Parameters.AddWithValue("@Work_Order_Number", Work_Order_Number);
        cmd.Parameters.AddWithValue("@DRUM_NUMBER", DRUM_NUMBER);
        cmd.Parameters.AddWithValue("@D10", D10);
        cmd.Parameters.AddWithValue("@D50", D50);
        cmd.Parameters.AddWithValue("@D90", D90);
        cmd.Parameters.AddWithValue("@D99", D99);
        cmd.Parameters.AddWithValue("@Remark", Remark);
        cmd.Parameters.AddWithValue("@IS_ACTIVE", true);
        cmd.Parameters.AddWithValue("@UPDATED_BY", HttpContext.Current.User.Identity.Name);
        cmd.Parameters.AddWithValue("@UPDATED_DATE", DateTime.Now);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }



    public int delete(String Particle_Size_ID)
    {
        int i = 0;
        string sql = "Delete FROM tbl_qcsicomill_particlesize WHERE Particle_Size_ID=@Particle_Size_ID";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@Particle_Size_ID", Particle_Size_ID);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }


    public List<tbl_qcsicomill_particlesize> ddlWorkOrderForPH()
    {
        List<tbl_qcsicomill_particlesize> list = new List<tbl_qcsicomill_particlesize>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select distinct Work_Order_Number from tbl_qcsicomill_particlesize Order By Work_Order_Number";

        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_qcsicomill_particlesize()
            {

                Work_Order_Number = dr["Work_Order_Number"].ToString(),

            });
        }

        return list;
    }


    public List<tbl_qcsicomill_particlesize> ddlDrumNumber(string WorkOrderNumber)
    {
        List<tbl_qcsicomill_particlesize> list = new List<tbl_qcsicomill_particlesize>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select distinct Drum_Number from tbl_qcsicomill_particlesize where Work_Order_Number=@Work_Order_Number and Drum_Number<>'' order by Drum_Number";
        cmd.Parameters.AddWithValue("@Work_Order_Number", WorkOrderNumber);
        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_qcsicomill_particlesize()
            {
                DRUM_NUMBER = dr["Drum_Number"].ToString()
            });
        }

        return list;
    }

}